package cn.group01.git.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.SupplierPageDTO;
import cn.group01.git.entity.Agent;
import cn.group01.git.entity.Supplier;
import cn.group01.git.mapper.SupplierMapper;
import cn.group01.git.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
/**
 * @description:
 * @author: 叶晓龙
 * @date: 2023/7/13 11:14
 **/
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    // 供应商名称下拉选项回显
    public ResponseResult supList() {
        List<Supplier> suppliers = supplierMapper.supList();
        System.out.println(suppliers);
        return ResponseResult.success(CodeMsg.SUCCESS,suppliers,null);
    }

    // 供应商页面回显
    public ResponseResult getList(SupplierPageDTO supplierPageDTO) {
        // 开启分页
        PageHelper.startPage(supplierPageDTO.getPage(), supplierPageDTO.getLimit());
        // 查询结果
        List<Supplier> suppliers = supplierMapper.getList(supplierPageDTO.getSupName());
        PageInfo<Supplier> supplierPageInfo = new PageInfo<Supplier>(suppliers);
        long total = supplierPageInfo.getTotal();
        return ResponseResult.success(CodeMsg.SUCCESS,suppliers,total);
    }

    // 添加供应商
    public ResponseResult addSupplier(Supplier supplier) {
        // 先查询供应商是否存在
        Supplier supplier1 = supplierMapper.getSupplierByName(supplier.getSupName());
        // 存在
     /*   if (supplier1!=null){
            return ResponseResult.fail(CodeMsg.SUPPLOER_ERROR);
        }*/
        if (!ObjectUtils.isEmpty(supplier1)){
            return ResponseResult.fail(CodeMsg.SUPPLIER_EXIST);
        }
        // 不存在就添加
        supplierMapper.addSupplier(supplier);
        return ResponseResult.success(CodeMsg.SUPPLOER_SUCCESS);
    }

    // 编辑界面数据回显
    public Supplier getById(Integer supId) {
        // 根据supId查询
        return supplierMapper.getById(supId);
    }

    // 编辑
    public ResponseResult editSupplier(Supplier supplier) {
        supplierMapper.editSupplier(supplier);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }

    // 删除
    public ResponseResult delete(Integer[] ids) {
        supplierMapper.delete(ids);
        return ResponseResult.success(CodeMsg.DELETE);
    }

    public ResponseResult batchAdd(MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseResult.fail(CodeMsg.EMPTY_FILE_ERROR);
        }
        //解析excel表格
        try {
            InputStream inputStream = file.getInputStream();
            //导入参数
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            importParams.setHeadRows(1);
            List<Supplier> suppliers = ExcelImportUtil.importExcel(inputStream, Supplier.class, importParams);
            for (Supplier supplier : suppliers) {
                String supplierName = supplier.getSupName();
                Supplier sup = supplierMapper.getSupplierByName(supplierName);
                if (!ObjectUtils.isEmpty(sup)) {
                    throw new RuntimeException("供应商重复");
                }
                //添加供应商信息到数据库
                supplierMapper.addSupplier(supplier);
            }
            return ResponseResult.success(CodeMsg.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务器异常");
        }
    }
}
